#include <QtCore/QCoreApplication>
#include <qapplication.h>
#include "OSCthulhuClient.h"
#include "OSCthulhuServer.h"
#include "QString"
#include "string"
#include "QSemaphore"
#include "gui/MainWindow.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <QPlastiqueStyle>
#include <QFile>
#include <QSettings>

//#include <syslog.h>
#include <string.h>
//stdin and stdout for interapplication communication
int main(int argc, char *argv[])
{
    QSemaphore* wait4CleanupDone = new QSemaphore(1);
    int r;
    if(argc>2) {
    if(std::string(argv[1]).compare("Server") == 0) {
       //Standard Server Mode
       QCoreApplication a(argc, argv);
       OSCthulhuServer* server = new OSCthulhuServer(atoi(argv[2]),QString(argv[3]));
       r = a.exec();
       wait4CleanupDone->acquire();
   } else if(std::string(argv[1]).compare("daemon") == 0) {
    #if defined(__WIN32__) || defined(WIN32)

    #else
       //Daemon Server Mode
       /* Our process ID and Session ID */
              pid_t pid, sid;

              /* Fork off the parent process */
              pid = fork();
              if (pid < 0) {
                      exit(EXIT_FAILURE);
              }
              /* If we got a good PID, then
                 we can exit the parent process. */
              if (pid > 0) {
                      exit(EXIT_SUCCESS);
              }

              /* Change the file mode mask */
              umask(0);

              /* Open any logs here */

              /* Create a new SID for the child process */
              sid = setsid();
              if (sid < 0) {
                      /* Log the failure */
                      exit(EXIT_FAILURE);
              }

              /* Change the current working directory */
              if ((chdir("/")) < 0) {
                      /* Log the failure */
                      exit(EXIT_FAILURE);
              }

              /* Close out the standard file descriptors */
              close(STDIN_FILENO);
              close(STDOUT_FILENO);
              close(STDERR_FILENO);

       QCoreApplication a(argc, argv);
       OSCthulhuServer* server = new OSCthulhuServer(atoi(argv[2]),QString(argv[3]));
       r = a.exec();
       wait4CleanupDone->acquire();
       exit(EXIT_SUCCESS);
       #endif
   }

   } else {
       //Client Mode

       QApplication a(argc, argv);
       a.setApplicationName("OSCthulhu");
       a.setApplicationVersion("2.0a");
       a.setOrganizationName("Curtis McKinney");
       a.setOrganizationDomain("curtismckinney.com");
       QFile styleFile(":/style/gui/cthulhuStyleSheet.qss");
       if(styleFile.open(QFile::ReadOnly))
       {
           QString styleSheet = QLatin1String(styleFile.readAll());
           qApp->setStyleSheet(styleSheet);
       }
       //a.setStyle(new QPlastiqueStyle);

       QSettings* settings = new QSettings();
       OSCthulhuClient* client = new OSCthulhuClient(
                   settings->value("serverAddress",QVariant("127.0.0.1")).toString(),
                   settings->value("serverPassword",QVariant("lagpass")).toString(),
                   settings->value("userName",QVariant("Hatchling")).toString(),
                   settings->value("userPassword",QVariant("fakePass")).toString(),
                   settings->value("serverPort",QVariant(32242)).toInt(),
                   settings->value("clientPort",QVariant(32243)).toInt(),
                   settings->value("appPort",QVariant(32244)).toInt(),
                   wait4CleanupDone,
                   settings
       );

       QObject::connect(&a, SIGNAL(aboutToQuit()), client, SLOT(handleQuit()));
       r = a.exec();
       wait4CleanupDone->acquire();
    }
    return r;
}
